﻿using BCCommon;
using BCCommon.TMS.MarketingCoupon;
using BCDto.TMS.MarketingCoupon.MarketingCoupon;
using BCDto.TMS.MarketingCoupon.MarketingCouponReceive;
using BCDto.TMS.MarketingCoupon.MarketingCouponType;
using BCDto.TMS.MarketingCoupon.MarketingCouponUsing;
using BCService.TMS.MarketingCoupon.MarketingCoupon;
using BCService.TMS.MarketingCoupon.MarketingCouponReceive;
using BCService.TMS.MarketingCoupon.MarketingCouponUsing;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace BCTMSRESTful.Controllers.ClientApp.MoveHouseConfig
{
    /// <summary>
    /// 搬家配置
    /// </summary>
    [ApiExplorerSettings(GroupName = "ClientApp")]
    [Route("api/v1/ClientApp/[controller]")]
    public class MarketingCouponController : ClientAppBaseController
    {
        private readonly IMarketingCouponService marketingCouponService;
        private readonly IMarketingCouponReceiveService marketingCouponReceiveService;
        private readonly IMarketingCouponUsingService marketingCouponUsingService;
        public MarketingCouponController(IMarketingCouponService marketingCouponService,
            IMarketingCouponReceiveService marketingCouponReceiveService,
            IMarketingCouponUsingService marketingCouponUsingService)
        {
            this.marketingCouponService = marketingCouponService;
            this.marketingCouponReceiveService = marketingCouponReceiveService;
            this.marketingCouponUsingService = marketingCouponUsingService;
        }

        /// <summary>
        /// 获取可领取的优惠券
        /// </summary>
        /// <param name="countyCode">区编码</param>
        /// <returns></returns>
        [HttpGet("[action]/{county_code}")]
        [ProducesResponseType(typeof(IEnumerable<MarketingCouponTypeDto>), 200)]
        public IActionResult GetCanReceiveList([FromRoute(Name = "county_code")] string countyCode)
        {
            var result = this.marketingCouponService.GetCanReceiveList(this.UserInfo, countyCode);
            return this.Ok(result);
        }

        /// <summary>
        /// 领取优惠券
        /// </summary>
        /// <param name="marketingCouponTypeId">优惠券类型id</param>
        /// <param name="countyCode">区编码</param>
        /// <returns></returns>
        [HttpPut("[action]/{marketing_coupon_type_id}/{county_code}")]
        [ProducesResponseType(typeof(bool), 200)]
        [Log("领取优惠券")]
        public IActionResult ReceiveMarketingCoupon([FromRoute(Name = "marketing_coupon_type_id")] long marketingCouponTypeId,
            [FromRoute(Name = "county_code")] string countyCode)
        {
            var result = this.marketingCouponService.ReceiveMarketingCoupon(this.UserInfo, marketingCouponTypeId, countyCode);
            return this.Ok(result);
        }

        /// <summary>
        /// 获取可以使用的优惠券
        /// </summary>
        /// <param name="vehicleTransportBusinessConfigId">运输类型id</param>
        /// <param name="countyCode">区编码</param>
        /// <returns></returns>
        [HttpGet("[action]/{vehicle_transport_business_config_id}/{county_code}")]
        [ProducesResponseType(typeof(IEnumerable<MarketingCouponDto>), 200)]
        public IActionResult GetCanUseList([FromRoute(Name = "vehicle_transport_business_config_id")] long vehicleTransportBusinessConfigId,
            [FromRoute(Name = "county_code")] string countyCode)
        {
            var result = this.marketingCouponService.GetCanUseList(this.UserInfo, vehicleTransportBusinessConfigId, countyCode);
            return this.Ok(result);
        }

        /// <summary>
        /// 使用优惠券
        /// </summary>
        /// <param name="marketingCouponId">优惠券id</param>
        /// <param name="countyCode">区编码</param>
        /// <param name="orderId">订单id</param>
        /// <returns></returns>
        [HttpPut("[action]/{marketing_coupon_id}/{county_code}/{order_id}")]
        [ProducesResponseType(typeof(bool), 200)]
        [Log("使用优惠券")]
        public IActionResult UseMarketingCoupon([FromRoute(Name = "marketing_coupon_id")] long marketingCouponId,
            [FromRoute(Name = "county_code")] string countyCode,
            [FromRoute(Name = "order_id")] long orderId)
        {
            var result = this.marketingCouponService.UseMarketingCoupon(this.UserInfo, marketingCouponId, countyCode, orderId);
            return this.Ok(result);
        }


        /// <summary>
        /// 获取用户优惠券列表(分页)
        /// </summary>
        /// <param name="marketingCouponStatus">优惠券状态</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<MarketingCouponDto>), 200)]
        public IActionResult GetMarketingCouponList(
            [FromQuery(Name = "marketing_coupon_status")] MarketingCouponStatusEnum? marketingCouponStatus,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)

        {
            var result = this.marketingCouponService.GetMarketingCouponList(this.UserInfo.UserId, marketingCouponStatus, pageIndex, pageSize);
            return this.Ok(result);
        }

        /// <summary>
        /// 删除过期的优惠券
        /// </summary>
        /// <param name="marketingCouponId">优惠券id</param>
        /// <returns></returns>
        [HttpDelete("{marketing_coupon_id}")]
        [ProducesResponseType(typeof(NoContentResult), 200)]
        public IActionResult Delete(
            [FromRoute(Name = "marketing_coupon_id")] long marketingCouponId)

        {
            var result = this.marketingCouponService.Delete(this.UserInfo.UserId, marketingCouponId);
            return NoContent();
        }

        /// <summary>
        /// 获取优惠券领取列表(分页)
        /// </summary>
        /// <param name="isInvalid">是否有效</param>
        /// <param name="isUse">是否可以当前使用</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<MarketingCouponReceiveDto>), 200)]
        public IActionResult GetReceiveRecord([FromQuery(Name = "is_invalid")] bool? isInvalid,
            [FromQuery(Name = "is_use")] bool? isUse,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)

        {
            var result = this.marketingCouponReceiveService.Query(null, this.UserInfo.UserId, isInvalid, isUse, pageIndex, pageSize);
            return this.Ok(result);
        }

        /// <summary>
        /// 获取优惠券使用列表(分页)
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<MarketingCouponUsingDto>), 200)]
        public IActionResult GetUsingRecord([FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            var result = this.marketingCouponUsingService.Query(null, this.UserInfo.UserId, pageIndex, pageSize);
            return this.Ok(result);
        }

    }
}
